package lib.translation;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.tagext.BodyContent;
import javax.servlet.jsp.tagext.BodyTagSupport;

import lib.base.EgoSystem;

@SuppressWarnings("serial")
public class TranslateTag extends BodyTagSupport 
{
	public int doAfterBody() throws JspException
	{
		try {
			BodyContent bodycontent = getBodyContent();
			String body = bodycontent.getString();
			JspWriter out = bodycontent.getEnclosingWriter();
			if (body != null)
			{
				String lang;
				if (pageContext.getSession().getAttribute("language") != null)
				{
					lang = pageContext.getSession().getAttribute("language").toString();
				} else
				{
					lang = "en";
				}
				out.print( EgoSystem.translator.get(lang).translate(body) );
			} else
			{
				out.print(body);
			}
		} catch(Exception ioe)
		{
			throw new JspException("Error:"+ioe.getMessage());
		}
		return SKIP_BODY;
	}
}
